package uiLayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import errorLayer.*;
import modelLayer.Staff;
import modelLayer.Subject;
import modelLayer.Room;
import modelLayer.TeachingBlock;
import controlLayer.StaffCtrl;
import controlLayer.SubjectCtrl;
import controlLayer.RoomCtrl;
import controlLayer.TeachingBlockCtrl;

public class TeachingBlockGUI extends JPanel {
	
	private TeachingBlockCtrl tbCtrl = new TeachingBlockCtrl();
	private StaffCtrl staffCtrl = new StaffCtrl();
	private SubjectCtrl subjectCtrl = new SubjectCtrl();
	private RoomCtrl roomCtrl = new RoomCtrl();
	private List<TeachingBlock> allTeachingBlocks = new ArrayList<>();
	private JComboBox allTB;
	private JPanel tbListingPlusEdit;
	private JButton addTBBtn;
	private JButton editTBBtn;
	private JButton removeBtn;
	private JTextField idField;
	private JTextField staffField;
	private JTextField subjectField;
	private JTextField roomField;
	
	public TeachingBlockGUI () {
		
		setLayout(new BorderLayout(0, 0));
		
		allTeachingBlocks = tbCtrl.getAllTeachingBlocks();
		
		setup();
		
	}

	private void setup() {
		
		tbListingPlusEdit = new JPanel();
		tbListingPlusEdit.setLayout(new BorderLayout(0, 0));
		tbListingPlusEdit.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "TeachingBlock"));
		add(tbListingPlusEdit, BorderLayout.NORTH);
		
		JPanel textNameArea = new JPanel();
		textNameArea.setLayout(new GridLayout(4, 1, 0, 5));
		tbListingPlusEdit.add(textNameArea, BorderLayout.WEST);
		
		JPanel listingTB = new JPanel();
		listingTB.setBorder(new EmptyBorder(5, 5, 5, 5));
		listingTB.setLayout(new GridLayout(4, 1, 0, 5));
		tbListingPlusEdit.add(listingTB, BorderLayout.CENTER);
		
		JPanel buttonArea = new JPanel();
		buttonArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonArea.setLayout(new GridLayout(4, 1, 0, 5));
		tbListingPlusEdit.add(buttonArea, BorderLayout.EAST);
		
		textNameArea.add(new JLabel("ID"));
		textNameArea.add(new JLabel("Staff"));
		textNameArea.add(new JLabel("Subject"));
		textNameArea.add(new JLabel("Room"));
		
		allTB = new JComboBox();
		allTB.setPreferredSize(new Dimension(150, 20));
		allTB.setMinimumSize(new Dimension(150, 20));
		allTB.setMaximumSize(new Dimension(150, 20));
		allTB.addItem("New Teaching Block");
		
		allTB.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if (e.getItem() == "New Teaching Block") {
					
					addTBBtn.setEnabled(true);
					editTBBtn.setEnabled(false);
					removeBtn.setEnabled(false);
					
					idField.setText("");
					staffField.setText("");
					subjectField.setText("");
					roomField.setText("");
				}
				
				else if (e.getItem() != "New Teaching Block" ) {
					
					addTBBtn.setEnabled(false);
					editTBBtn.setEnabled(true);
					removeBtn.setEnabled(true);
					
					try {
						
						TeachingBlock tb = (TeachingBlock) e.getItem();
						
						idField.setText("" + tb.getID());
						staffField.setText(tb.getStaff().getName());
						subjectField.setText(tb.getSubject().getName());
						roomField.setText(tb.getRoom().getName());
						
					}
					catch (Exception exception) {
						System.out.println("Exception " + exception);
					}
					
				}
				
			}
		});
		
		for (int i = 0; i < allTeachingBlocks.size(); ++i) {
			
			TeachingBlock tb = allTeachingBlocks.get(i);
			allTB.addItem(tb);
			
		}
		
		if (allTeachingBlocks.size() == 0) {
			allTB.addItem("No Teaching Blocks");
		}
		
		buttonArea.add(allTB);
		
		addTBBtn = new JButton("Add");
		
		buttonArea.add(addTBBtn);
		
		editTBBtn = new JButton("Edit");
		editTBBtn.setEnabled(false);
		
		buttonArea.add(editTBBtn);
		
		removeBtn = new JButton("Remove");
		removeBtn.setEnabled(false);
		
		buttonArea.add(removeBtn);
		
		idField = new JTextField();
		idField.setEditable(false);
		staffField = new JTextField();
		subjectField = new JTextField();
		roomField = new JTextField();
		
		listingTB.add(idField);
		listingTB.add(staffField);
		listingTB.add(subjectField);
		listingTB.add(roomField);
		
		addTBBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				TeachingBlock tb = new TeachingBlock();
				
				if (!staffField.getText().isEmpty() && !subjectField.getText().isEmpty() && !roomField.getText().isEmpty()) {
					
					Staff staff = null;
					Subject subject = null;
					Room room = null;
					
					if (integerChecker(staffField.getText())){
						
						staff = staffCtrl.findStaffID(Integer.parseInt(staffField.getText()));
						
					}
					else if (!integerChecker(staffField.getText())){
						
						staff = staffCtrl.findSingleStaffName(staffField.getText());
						
					}
					
					if (integerChecker(subjectField.getText())){
						
						subject = subjectCtrl.findSubjectID(Integer.parseInt(subjectField.getText()));
						
					}
					else if (!integerChecker(subjectField.getText())) {
						
						subject = subjectCtrl.findSubjectName(subjectField.getText());
						
					}
					
					if (integerChecker(roomField.getText())) {
						
						room = roomCtrl.findRoomID(Integer.parseInt(roomField.getText()));
						
					}
					else if (!integerChecker(roomField.getText())) {
						
						room = roomCtrl.findRoomName(roomField.getText());
						
					}
					
					if (staff != null && subject != null && room != null) {
						
						tb.setStaff(staff);
						tb.setSubject(subject);
						tb.setRoom(room);
						
						if (allTB.getItemAt(1) == "No Teaching Blocks") {
							allTB.removeItemAt(1);
						}
						
						if (tbCtrl.saveTeachingBlock(tb)) {
							
							tb.setID(tbCtrl.getLatestTBID());
							
							allTB.addItem(tb);
							allTB.setSelectedIndex((allTB.getItemCount() - 1));	
							
						}
						else {
							
							new ExistingTeachingBlockError();
							
							TeachingBlock test = new TeachingBlock();
							
							for (int i = 1; i < allTB.getItemCount(); i++) {
								
								test = (TeachingBlock) allTB.getItemAt(i);
								
								if (test.getID() == tbCtrl.existingTB().get(0)) {
									allTB.setSelectedIndex(i);
								}
								
							}
							
						}
						
					}
					else {
						
						if (staff == null) {
							new notFound("Staff member " + staffField.getText());
						}
						if (subject == null) {
							new notFound("Subject " + subjectField.getText());
						}
						if (room == null) {
							new notFound("Room " + roomField.getText());
						}
						
					}
					
				}
				else {
					if (staffField.getText().isEmpty()) {
						new EmptyField("Staff");
					} 
					
					if (subjectField.getText().isEmpty()) {
						new EmptyField("Subject");
					}
					
					if (roomField.getText().isEmpty()) {
						new EmptyField("Room");
					}
					
				}
				
			}
		});
		
		editTBBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				TeachingBlock tb = new TeachingBlock();
				
				if (!staffField.getText().isEmpty() && !subjectField.getText().isEmpty() && !roomField.getText().isEmpty()) {
					
					Staff staff = null;
					Subject subject = null;
					Room room = null;
					
					if (integerChecker(staffField.getText())){
						
						staff = staffCtrl.findStaffID(Integer.parseInt(staffField.getText()));
						
					}
					else if (!integerChecker(staffField.getText())){
						
						staff = staffCtrl.findSingleStaffName(staffField.getText());
						
					}
					
					if (integerChecker(subjectField.getText())){
						
						subject = subjectCtrl.findSubjectID(Integer.parseInt(subjectField.getText()));
						
					}
					else if (!integerChecker(subjectField.getText())) {
						
						subject = subjectCtrl.findSubjectName(subjectField.getText());
						
					}
					
					if (integerChecker(roomField.getText())) {
						
						room = roomCtrl.findRoomID(Integer.parseInt(roomField.getText()));
						
					}
					else if (!integerChecker(roomField.getText())) {
						
						room = roomCtrl.findRoomName(roomField.getText());
						
					}
					
					if (staff != null && subject != null && room != null) {
						
						tb.setStaff(staff);
						tb.setSubject(subject);
						tb.setRoom(room);
						
						if (tbCtrl.updateTeachingBlock(tb)) {
							
							tb.setID(Integer.parseInt(idField.getText()));
							
							allTB.removeItemAt(allTB.getSelectedIndex());
							allTB.addItem(tb);	
							
						}
						else {
							
							new ExistingTeachingBlockError();
							
							TeachingBlock test = new TeachingBlock();
							
							for (int i = 1; i < allTB.getItemCount(); i++) {
								
								test = (TeachingBlock) allTB.getItemAt(i);
								
								if (test.getID() == tbCtrl.existingTB().get(0)) {
									allTB.setSelectedIndex(i);
								}
								
							}
							
						}
						
					}
					
				}
			}
		});
		
		removeBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent event) {
				
				String deletion = "TeachingBlock: " + idField.getText() + ", " + staffField.getText() + ", " + subjectField.getText() + ", " + roomField.getText(); 
				
				DeleteWarning dw = new DeleteWarning(deletion);
				
				if(dw.getAnswer()) {
					tbCtrl.deleteTeachingBlockID(Integer.parseInt(idField.getText()));
					allTB.removeItemAt(allTB.getSelectedIndex());
				}
				
			}
			
		});
		
	}
	
	public boolean integerChecker (String testString) {
		
		try{
			
			Integer.parseInt(testString);
	         return true;
			
		}
		catch (NumberFormatException e) {
			
			return false;
			
		}
	}
	
	public void dialogFix(Component com1, Component com2) {
		
		JPanel dialogFix = new JPanel();
		dialogFix.setLayout(new FlowLayout(FlowLayout.RIGHT));
		tbListingPlusEdit.add(dialogFix, BorderLayout.SOUTH);
		dialogFix.add(com1);
		dialogFix.add(com2);
		
	}
	
	public TeachingBlock getSelection() {
		
		TeachingBlock tb = (TeachingBlock) allTB.getSelectedItem();
		
		return tb;
		
	}
	
	public int getSelectedID () {
		return Integer.parseInt(idField.getText());
	}
	
	public String getSelectedStaff () {
		return staffField.getText();
	}
	
	public String getSelectedSubject () {
		return subjectField.getText();
	}
	
	public String getSelectedRoom () {
		return roomField.getText();
	}
	
	
}
